package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrderDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired(required = false)
    private OrdersService ordersService;

    @RequestMapping("/submit")
    public R<String> submit(@RequestBody Orders orders, HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        ordersService.add(orders,userId);
        return R.success("下单成功");
    }
    @RequestMapping("/userPage")
    public R<Page<OrderDto>> userPage(Integer page,Integer pageSize,HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        Page<OrderDto> orderDtoList = ordersService.findOrderAndOrderDetail(page, pageSize, userId);
        return R.success(orderDtoList);
    }
}
